package com.lbt.eduservice.controller;


import com.lbt.commonuntils.R;
import com.lbt.eduservice.entity.EduChapter;
import com.lbt.eduservice.entity.EduCourse;
import com.lbt.eduservice.entity.chapter.ChapterVo;
import com.lbt.eduservice.entity.vo.CourseInfoVo;
import com.lbt.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-02
 */
@Api(description = "章节管理")
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    EduChapterService chapterService;
    @ApiOperation(value = "获取章节信息")
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable(value = "courseId") String courseId){
        List<ChapterVo>list= chapterService.getChapterVideoByCourseID(courseId);
        return R.ok().data("allChapterVideo",list);
    }
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter character){
        //更新这块有点问题
      //  System.out.println(character.toString());
        chapterService.updateById(character);
        return R.ok();
    }
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter character){
        System.out.println("我执行了");
        chapterService.save(character);
        return R.ok();
    }
    //删除的时候就要把章节下面的小接 全部删除
    //1、删除完小节才给删除章节  2、删除章节的时候把小节附带删除光
    //做的是如果章节下面有小节  提示先删除完小节才能删除章节
    @DeleteMapping("/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){

        boolean flage = chapterService.deleteChapter(chapterId);
        if(flage){
            return R.ok();
        }
       return R.error().data("msg","请先删除小节");
    }

    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",chapter);
    }
}

